/*
Source : https://leetcode.com/problems/reverse-integer/
Author : nflush@outlook.com
Date   : 2016-05-03
*/

/*
7. Reverse Integer  
Total Accepted: 137357 Total Submissions: 581474 Difficulty: Easy

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Subscribe to see which companies asked this question
*/

int reverse(int x) {
    if (x >= 0 && x < 10){
        return x;
    }
    unsigned long int ret;
    char array[36] = {0};
    sprintf(array, "%d", x<0?0-x:x);
    int len = strlen(array);
    int i;
    for(i = 0; i < len/2;i++){
        char ch = array[i];
        array[i] = array[len - i - 1];
        array[len - i - 1] = ch;
    }
    sscanf(array, "%lu", &ret);
    if (ret > INT_MAX){
        ret = 0;
    }
    return x <0 ? 0 - ret : ret;
    
}

int reverse(int x) {
    if (x >= -9 && x < 10){
        return x;
    }
    unsigned long int ret = 0;
    unsigned long int y = x<0?0-x:x;
    for (;y>0;y/=10){
        ret = y %10 + ret * 10;
    }
    if (ret > INT_MAX){
        ret = 0;
    }
    return x <0 ? 0 - ret : ret;
}

int reverse(int x) {
    if (x >= -9 && x < 10){
        return x;
    }
    unsigned long int ret;
    char array[36] = {0};
    sprintf(array, "%d", x<0?0-x:x);
    strrev(array);
    sscanf(array, "%lu", &ret);
    if (ret > INT_MAX){
        ret = 0;
    }
    return x <0 ? 0 - ret : ret;

}

